package com.jack.submeiju.database;


import com.jack.submeiju.entity.TvInfo;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class TvInfoDb {

	public static void deleteTvInfo(TvInfo info) throws Exception {
		Connection conn = ConnectionManager.getSqlConnnection();
		if (conn == null) {
			throw new Exception("jdbc connection is null!");
		}
		
		String sql = "update tvinfo set deleted=1 where id=?";
		PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
		ps.setInt(1, info.getId());
		
		ps.executeUpdate();
		ps.close();
		conn.close();
	}

	public static void updateTvInfo(TvInfo info) throws Exception {
		Connection conn = ConnectionManager.getSqlConnnection();
		if (conn == null) {
			throw new Exception("jdbc connection is null!");
		}

		String sql = "update tvinfo set tv_name_ch=?,tv_name_en=?,tv_summary=?,tv_start_date=?,tv_language=?,tv_class=?,tv_contry=?,tv_pic_path=? where id=?";
		PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
		ps.setString(1, info.getTvNameCh());
		ps.setString(2, info.getTvNameEn());
		ps.setString(3, info.getTvSummary());
		ps.setString(4, info.getTvStartDate());
		ps.setString(5, info.getTvLanguage());
		ps.setString(6, info.getTvClass());
		ps.setString(7, info.getTvCountry());
		ps.setString(8, info.getTvPicPath());

		ps.setInt(9, info.getId());
		ps.executeUpdate();
		ps.close();
		conn.close();
	}

	public static void addTvInfo(TvInfo info) throws Exception {
		Connection conn = ConnectionManager.getSqlConnnection();
		if (conn == null) {
			throw new Exception("jdbc connection is null!");
		}

		String sql = "insert into tvinfo(tv_name_ch,tv_name_en,tv_summary,tv_start_date,tv_language,tv_class,tv_contry,tv_pic_path) "
				+ "values(?,?,?,?,?,?,?,?) ";
		PreparedStatement pst = (PreparedStatement) conn.prepareStatement(sql);

		pst.setString(1, info.getTvNameCh());
		pst.setString(2, info.getTvNameEn());
		pst.setString(3, info.getTvSummary());
		pst.setString(4, info.getTvStartDate());
		pst.setString(5, info.getTvLanguage());
		pst.setString(6, info.getTvClass());
		pst.setString(7, info.getTvCountry());
		pst.setString(8, info.getTvPicPath());

		int result = pst.executeUpdate();
		pst.close();
		conn.close();
		if (result <= 0) {
			throw new Exception("error occurs on insert a TvInfo to db.");
		}
	}

}
